<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <!--公用头部-->
    <th:block th:replace="common/head::commonHead('站内消息修改-小邑')" />
    <style>
        #formAdvForm .layui-row {
            margin-top: 20px;
        }

        #formAdvForm .layui-form-item {
            margin-bottom: 0;
        }

        #formAdvForm .layui-form-item .layui-inline {
            margin-bottom: 25px;
            margin-right: 0;
        }
    </style>
</head>
<!--公用js-->
<th:block th:replace="common/head::commonJs"/>
<body marginwidth="0" marginheight="0">
<form class="layui-form" action="" id="formAdvForm" lay-filter="formAdvForm">
    <div class="layui-fluid  layui-fluid-btn">
        <div class="layui-col-md12">
            <div class="layui-card" style="margin: 10px;">
                <div class="layui-card-body">
                    <input name="id" type="text" style="display: none">
                    <div class="layui-form-item  layui-row">
                        <div class="layui-inline layui-col-md12">
                            <label class="layui-form-label layui-form-required">类型</label>
                            <div class="layui-input-block" id="type">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide" id="accept_type">
                        <div class="layui-inline layui-col-md12">
                            <label class="layui-form-label">接收者</label>
                            <div class="layui-input-block">
                                <input type="text" name="acceptId" id="acceptId" autocomplete="off"
                                       class="layui-input layui-hide">
                                <input type="text" name="accept" readonly id="accept" onclick="selectUser()" placeholder="请选择接收者" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline layui-col-md12">
                            <label class="layui-form-label layui-form-required">标题</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" id="title" lay-verify="required" placeholder="请输入标题"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline layui-col-md12">
                            <label class="layui-form-label layui-form-required">内容</label>
                            <div class="layui-input-block">
                                <textarea type="text" name="content" id="content" lay-verify="required"
                                          placeholder="请输入内容"
                                          autocomplete="off" class="layui-textarea"></textarea>
                            </div>
                        </div>
                    </div>


                    <div class="form-group-bottom">
                        <button class="xy-btn xy-btn-primary subCheckBtn" lay-submit lay-filter="formBtn">提交</button>
                        <button type="reset" class="xy-btn ">重置</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    /*选择用户*/
    function selectUser(){
        var selectIds = [];
        var selectNames = [];
        var callback = function (checkStatus) {
            checkStatus.forEach(function (obj){
                selectIds.push(obj.id);
                selectNames.push(obj.username);
            })
            $("#acceptId").val(selectIds.toString());
            $("#accept").val(selectNames.toString());
        }
        chooseUser(1,callback);
    }
    layui.use(['layer', 'jquery', 'form', 'notice', 'xmSelect'], function () {
        var $ = layui.jquery
            , form = layui.form
            , notice = layui.notice
            , xmSelect = layui.xmSelect
            , layer = layui.layer;

        /*类型下拉*/
        var type ;

        //获取详情信息，填充表单
        sendAjaxPostToError(
            basePath + '/notice/detailNotice',
            {
                id: utils.request.getQueryString('id')
            },
            function (res){
                if (res.success){
                    form.val('formAdvForm', res.data);
                    type = xmSelect.render({
                        el: '#type',
                        tips: "类型",
                        name: 'type',
                        initValue:[res.data.type],
                        radio: true,
                        clickClose: true,
                        theme: {color: '#8799a3',},
                        model: {
                            label: {type: 'text'},
                            icon: "hidden"
                        },
                        data: initSelectData("sys_notice_type"),
                        on: function (data) {
                            if (data.isAdd) {
                                var item = data.change[0];
                                if (item != null) {
                                    if (item.value == "private") {
                                        $("#accept_type").removeClass("layui-hide");
                                        $("#accept_type label").addClass("layui-form-required");
                                        $("#accept").attr("lay-verify", "required");
                                    } else {
                                        $("#accept_type").addClass("layui-hide");
                                        $("#accept_type label").removeClass("ayui-form-required");
                                        $("#accept").removeAttr("lay-verify");
                                    }
                                }
                            }
                        },
                    });
                    if (res.data.type == "private") {
                        $("#accept_type").removeClass("layui-hide");
                        $("#accept_type label").addClass("layui-form-required");
                        $("#accept").attr("lay-verify", "required");
                    } else {
                        $("#accept_type").addClass("layui-hide");
                        $("#accept_type label").removeClass("layui-form-required");
                        $("#accept").removeAttr("lay-verify");
                    }
                }else {
                    top.layui.notice.msg(res.msg ,{icon: 2,position:"topCenter"});
                }
            },
            function (e){
                top.layui.notice.msg("获取数据失败~" ,{icon: 2,position:"topCenter"});
            }
        );

        //监听提交
        form.on('submit(formBtn)', function(data) {
            var formDate = data.field;
            if (isEmpty(formDate.id)) {
                formDate.id = id;
            }
            var loadIndex = null;
            checkSubBtn(1);
            sendAjaxPostToCallback(
                basePath + '/notice/editNotice',
                formDate,
                function (result){
                    if (result.success){
                        // 提示信息
                        top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                        // 获取当前iframe层的索引
                        var index = parent.layer.getFrameIndex(window.name);
                        // 关闭弹窗
                        parent.layer.close(index);
                        // 重载表格
                        parent.layui.table.reload('noticeTable',{});
                    }else{
                        checkSubBtn(2);
                        top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                    }
                },
                function (data) {
                    checkSubBtn(2);
                    if (utils.string.isEmpty(data) && utils.string.isEmpty(data.msg)){
                        top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                    }else{
                        top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                    }
                },
                function (){
                    loadIndex = layer.load(1);
                },
                function (){
                    layer.close(loadIndex);
                },
            )
            return false;
        });

    });
</script>
</body>
</html>
